﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using APMAWebConsole.BusinessLogicServiceReference;

namespace APMAWebConsole.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void LoginButton_OnCommand(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Login":
                    {
                        var validateResult = Validate(UserName.Text, Password.Text);

                        if (validateResult > 0)
                        {
                            Session[SiteMaster.IsAdminKey] = validateResult == 1;

                            FormsAuthentication.RedirectFromLoginPage(UserName.Text, RememberMe.Checked);
                        }
                        else
                        {
                            FailureText.Text = "Invalid credentials. Please try again.";
                        }

                        break;
                    }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        private int Validate(string login, string password)
        {
            return SiteMaster.Proxy.ValidateUser(login, password);
        }
    }
}
